﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CodeIn.Diagnostics {
	public class AsyncStaticMessageHandler : BaseMessageHandler {
		public string Log { get; private set; }
		public int LogMaxLength { get; set; }

		public AsyncStaticMessageHandler() {
			Log = "";
			LogMaxLength = 1024000;
		}

		protected override void WriteOverride(string message) {
			Log += message;
			if (Log.Length > LogMaxLength)
				Log = Log.Substring(Log.Length - LogMaxLength, LogMaxLength);
			RaiseMessageWrittenEvent(message);
		}

		// ----------------------------------------------------------------
		// Custom Events
		// ----------------------------------------------------------------

		public event EventHandler<MessageWrittenEventArgs> MessageWritten;
		private void RaiseMessageWrittenEvent(string message) {
			if (MessageWritten != null)
				MessageWritten(this, new MessageWrittenEventArgs(message));
		}

		public class MessageWrittenEventArgs : EventArgs {
			public string Message { get; private set; }
			public MessageWrittenEventArgs(string message) {
				Message = message;
			}
		}
	}
}
